package com.yunbiji.controller;

import java.util.List;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yunbiji.model.Calendar;
/**
 * 待办模块操作类
 * @author 何瑞       
 *
 */
public class CalendarController extends Controller {
	private Integer id;// 事件id
	private String title;// 事件标题
	private String start;// 事件开始时间
	private String end;// 结束时间
	private Integer allDay;// 是否为全天事件
	private String color;// 事件的背景色
	private String tel;//关联的用户电话号码
	/**
	 * 返回fullcalendar主界面
	 */
public void index(){
//	render("/fullcalendar/json.html");
	render("schedule.html");
}
//查找calendar表中的时间，并以json格式返回到前端调用页面
/**
 * 用于监听fullcalendar上的点击事件
 */
@ActionKey("/findEvents")
public void findEvents(){
	String uname=getSessionAttr("username");
List<Record> calendar=Db.find("select * from calendar where tel=?",uname);
	renderJson(calendar);
} 

public String getTitle(){

	return title;
}
public String getStart(){
	return start;
}
public String getEnd(){
	return end;
}
public Integer getAllday(){
	return allDay;
}
public String getColor(){
	return color;
}

public Integer getId() {
	return id;
}
public String getTel(){
	return tel;
}

public void setId(Integer id) {
	this.id = id;
}



public void setTitle(String title) {
	this.title = title;
}



public void setStart(String start) {
	this.start = start;
}



public void setEnd(String end) {
	this.end = end;
}

public Integer getAllDay() {
	return allDay;
}

public void setAllDay(Integer allDay) {
	this.allDay = allDay;
}



public void setColor(String color) {
	this.color = color;
}
public void setTel(String tel){
	this.tel=tel;
}
/**
 * 用于插入fullcalendar事件
 */
@ActionKey("/insertEvent")
public void insertEvent(){
	String action = getPara("action");
	if("add".equals(action)){
		String events = getPara("event");//事件内容
		String isallday = getPara("isallday");//是否是全天事件
		String isend = getPara("isend");//是否有结束时间
		String startdate = getPara("startdate");
		String enddate = getPara("enddate");
		String s_time = getPara("s_hour") + ":" + getPara("s_minute") + ":00";
		String e_time = getPara("e_hour") + ":" + getPara("e_minute") + ":00";
		String tel=getSessionAttr("username");
		String start = "";
		String end = "";
		if("1".equals(isallday) && "1".equals(isend)){
			start = startdate;
			end = enddate;
		}else if("1".equals(isallday) && isend == null){
			start = startdate;
		}else if(isallday == null && "1".equals(isend)){
			start = startdate + " " + s_time;
			end = enddate + " " + e_time;
			isallday = "0";
		}else {
			start = startdate + " " + s_time;
			isallday = "0";
		}
		
		String[] colors = {"#360","#f30","#06c"};
		int index = (int)(Math.random()*colors.length);
		CalendarController calendar = new CalendarController();
		calendar.setTitle(events);
		calendar.setStart(start);
		calendar.setEnd(end);
		calendar.setAllDay(Integer.parseInt(isallday));
		calendar.setColor(colors[index]);
		calendar.setTel(tel);
		boolean b = new Calendar().add(calendar);
		if(b){
			renderText("1");
		}else {
			renderText("写入失败！");
		}
	}
}
/**
 * 用于更新fullcalendar事件
 */
public void updateEventAndDel(){
	String action = getPara("action");
 if("edit".equals(action)) {
		Integer id =  Integer.parseInt(getPara("id"));
		String events = getPara("event");//事件内容
		String isallday = getPara("isallday");//是否是全天事件
		String isend = getPara("isend");//是否有结束时间
		String startdate = getPara("startdate");
		String enddate = getPara("enddate");
		String s_time = getPara("s_hour") + ":" + getPara("s_minute") + ":00";
		String e_time = getPara("e_hour") + ":" + getPara("e_minute") + ":00";
		String tel=getSessionAttr("username");
		String start = "";
		String end = "";
		if("1".equals(isallday) && "1".equals(isend)){
			start = startdate;
			end = enddate;
		}else if("1".equals(isallday) && isend == null){
			start = startdate;
		}else if(isallday == null && "1".equals(isend)){
			start = startdate + " " + s_time;
			end = enddate + " " + e_time;
			isallday = "0";
		}else {
			start = startdate + " " + s_time;
			isallday = "0";
		}
		
		String[] colors = {"#360","#f30","#06c"};
		int index = (int)(Math.random()*colors.length);
		CalendarController calendar = new CalendarController();
		calendar.setTitle(events);
		calendar.setStart(start);
		calendar.setEnd(end);
		calendar.setAllDay(Integer.parseInt(isallday));
		calendar.setColor(colors[index]);
		calendar.setId(id);
		calendar.setTel(tel);
		boolean b = new Calendar().modify(calendar);
		if(b){
			renderText("1");
		}else {
			renderText("写入失败！");
		}
	}
 else if("del".equals(action)){
		Integer id =  Integer.parseInt(getPara("id"));
		if(id > 0){
			boolean b = new Calendar().del(id);
			if(b){
				renderText("1");
			}else {
				renderText("删除失败！");
			}
		}else {
			renderText("事件不存在！");
		}
	}

	
}
}


